package com.github.pig.admin.service.zhgl.notification.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.zhgl.notification.INotificationInfoMapper;
import com.github.pig.admin.model.zhgl.notification.NotificationInfo;
import com.github.pig.admin.service.sys.SysFileService;
import com.github.pig.admin.service.zhgl.notification.NotificationInfoService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 通知公告 服务实现类
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-10
 */
@Service
public class NotificationInfoServiceImpl extends ServiceImpl<INotificationInfoMapper, NotificationInfo> implements NotificationInfoService {

    @Value("${upload.path}")
    private String path;

    @Autowired
    private SysFileService sysFileService;
    @Autowired
    private INotificationInfoMapper INotificationInfoMapper;

    @Override
    public Page<NotificationInfo> selectPages(Page<NotificationInfo> page,NotificationInfo notificationInfo) {

        List<NotificationInfo> pageList = INotificationInfoMapper.selectPages(page,notificationInfo);
        return page.setRecords(pageList);
    }

    @Override
    public Boolean update(NotificationInfo notificationInfo) {
        getNotificationInfoBody(notificationInfo);
        return this.updateById(notificationInfo);
    }

    /**
     * 返回富文本内容-转化图片存储形式
     * @return
     */
    private void getNotificationInfoBody(NotificationInfo notificationInfo){
        if (StringUtils.isNotBlank(notificationInfo.getBody())) {
            Document doc = Jsoup.parse(notificationInfo.getBody());
            Elements imageList = doc.select("img"); //处理的切入点，找img标签
            if (imageList != null && imageList.size() > 0) { //图片
                for (Element e : imageList) {
                    String image = e.toString();
                    image = image.replace("<img src=\"","");
                    image = image.replace("\">", "");
                    String[] rep = image.split("base64,");
                    image = rep[1];
                    String imgType = "jpg";
                    if(StringUtils.isNotBlank(rep[0])){
                       imgType = rep[0].substring(rep[0].lastIndexOf("/")+1,rep[0].length()-1);
                    }
                    BASE64Decoder decoder = new BASE64Decoder();
                    long imgName = new Date().getTime();
                    e.attr("src",imgName + "."+imgType);
                    try {
                        byte[] bytes = decoder.decodeBuffer(image);
                        for(int j=0;j<bytes.length;++j)
                        {
                            if(bytes[j]<0)
                            {//调整异常数据
                                bytes[j]+=256;
                            }
                        }
                        File realfile = new File(path);
                        //创建目录
                        if (!realfile.exists()) {
                            realfile.mkdirs();
                        }
                        FileOutputStream write = new FileOutputStream(realfile + "/" + imgName+"."+imgType);
                        write.write(bytes);
                        write.close();
                        notificationInfo.setPicTop(imgName+"."+imgType);

                    } catch (IOException w) {
                        w.printStackTrace();
                    }
                }
                notificationInfo.setBody(doc.toString());
            }
        }
    }
    /**
     * 发布通知
     * @param notificationInfo
     * @return
     */
    @Override
    @Transactional
    public AjaxJson add(NotificationInfo notificationInfo) {
        AjaxJson json = new AjaxJson();
        getNotificationInfoBody(notificationInfo);
        this.insert(notificationInfo);
        json.setSuccess(true);
        json.setMsg("添加成功");
        return json;

    }

}
